package com.codi.bus.core.service.impl;

import com.codi.base.util.ListUtil;
import com.codi.bus.core.dao.FundWarehouseMappingDao;
import com.codi.bus.core.dao.FundWarehouseTransferDao;
import com.codi.bus.core.domain.FundWarehouseMapping;
import com.codi.bus.core.domain.FundWarehouseTransfer;
import com.codi.bus.core.resultModel.FundWarehouseTransferResult;
import com.codi.bus.core.service.FundWarehouseTransferService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("fundWarehouseTransferService")
public class FundWarehouseTransferServiceImpl implements FundWarehouseTransferService{
    @Resource
    private FundWarehouseTransferDao fundWarehouseTransferDao;

    @Resource
    private FundWarehouseMappingDao fundWarehouseMappingDao;

    @Override
    public Page<FundWarehouseTransfer> queryByPage(Pageable pageable) {
        return fundWarehouseTransferDao.queryByPage(pageable);
    }

    @Override
    @Transactional(readOnly = false)
    public int addFundWarehouseTransfer(FundWarehouseTransfer transfer) {
        transfer.setCreateDate(new Date());

        return fundWarehouseTransferDao.addFundWarehouseTransfer(transfer);
    }

    @Override
    public FundWarehouseTransfer selectOne(Long wareHouseId) {
        return fundWarehouseTransferDao.selectOne(wareHouseId);
    }

    @Override
    @Transactional(readOnly = false)
    public int deleteByKey(Long wareHouseId) {
        return fundWarehouseTransferDao.deleteByKey(wareHouseId);
    }

    @Override
    @Transactional(readOnly = false)
    public int updateOne(FundWarehouseTransfer transfer) {
        return fundWarehouseTransferDao.updateOne(transfer);
    }

    @Override
    public List<FundWarehouseTransferResult> queryTransfers(String combinationCode) {
        List<FundWarehouseTransfer> transfers = fundWarehouseTransferDao.queryByCombinationCode(combinationCode);

        if (ListUtil.isEmpty(transfers)) {
            return null;
        }

        List<FundWarehouseTransferResult> results = new ArrayList<>();

        for (FundWarehouseTransfer transfer : transfers) {
            List<FundWarehouseMapping> mappings = fundWarehouseMappingDao.queryByWareHouseId(transfer.getWarehouseId());

            FundWarehouseTransferResult result = new FundWarehouseTransferResult();
            BeanUtils.copyProperties(transfer, result);

            result.setMappings(mappings);

            results.add(result);
        }

        return results;
    }

    @Override
    public FundWarehouseTransferResult queryRencentTransfer(String combinationCode) {
        FundWarehouseTransfer transfer = fundWarehouseTransferDao.queryOneByCombinationCode(combinationCode);

        if (transfer == null) {
            return null;
        }

        List<FundWarehouseMapping> mappings = fundWarehouseMappingDao.queryByWareHouseId(transfer.getWarehouseId());

        FundWarehouseTransferResult result = new FundWarehouseTransferResult();
        BeanUtils.copyProperties(transfer, result);
        result.setMappings(mappings);

        return result;
    }
}
